{extend name="lib/common_list" /}
{block name="title"}{/block}
{block name="css"}
{load href="/static/sys/css/common.css" /}
{load href="/static/plugins/select2/css/select2.css" /}
{load href="/static/sys/css/skin_0.css" /}
<style>
    .upfile i{padding-right:3px !important;}
    .file{ position:absolute;right:0;top:0;width:100px;height:100px;opacity:0; ilter:alpha(opacity=0);cursor:pointer;margin-bottom: 0px;margin-right: 0px; }
</style>
{/block}
{block name="javascript"}
{load  href="/static/plugins/select2/js/select2.js" /}
<script>
    $(document).ready(function () {
        $("#ok").bind("click", Save);
        $("#cancel").bind("click", function () {
            parent.layer.close(parent.layer_image_add);
        });

        /**
         * 保存内容
         */
        function Save() {
            var url = $.trim($("input[name=url]").val());
            var image_cover1 = $.trim($("input[name=image_cover1]").val());
            if (url.length == 0 && image_cover1.length == 0) {
                layer.tips('请输入图片外链地址或选择上传', 'input[name=url]', {tips: [2, '#78BA32']});
                return false;
            }

            var load_index = layer.load(0, { shade: false });
            $.ajax({
                type: 'POST',
                url: "{:url('upload/image')}",
                data: $("#fmNew").serialize(),
                dataType: 'json',
                success: function (data) {
                    if (data.code == 1) {
                        layer.close(load_index);
                        layer.alert('添加成功', function () {
                            parent.getImageUrl(data.data);
                            //parent.layer.close(parent.layer_image_add);
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        });
                    } else {
                        layer.alert(data.msg);
                    }
                }
            })
        }

        // 上传图片
        var fileEls = document.getElementById('file');
        var fullBase64;
        var base64;
        $("#file").change(function (evt) {
            $("input[name='cover_url']").val('');
            if (!window.FileReader) return;
            var files = evt.target.files;
            var suffix = $(this).attr("suffix");

            for (var i = 0, f; f = files[i]; i++) {
                if (!f.type.match('image.*')) {
                    continue;
                }
                var imgagetype = files[i].type;
                var reader = new FileReader();
                reader.onload = (function (theFile) {
                    return function (e) {
                        fullBase64 = e.target.result;
                        appendFile(fullBase64, imgagetype, suffix);
                    };
                })(f);
                reader.readAsDataURL(f);
                $("input[name='url']").val('');
            }
        });

        function appendFile(entry, imgagetype, suffix) {
            var img = new Image();
            img.src = entry;

            img.onload = function () {
                var that = this;
                /*图片不压缩*/
                var w = that.width, h = that.height;

                //生成canvas
                var canvas = document.createElement('canvas');
                var ctx = canvas.getContext('2d');
                $(canvas).attr({width: w, height: h});
                ctx.drawImage(that, 0, 0, w, h);
                base64 = canvas.toDataURL(imgagetype, 1);
                document.getElementById('previewImg_' + suffix).src = base64;
                document.getElementById('image_' + suffix).value = base64;
            }
        }
    });


</script>
{/block}

{block name="content"}
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form method="post" id="fmNew" class="form-horizontal" enctype="multipart/form-data">

                        <div class="form-group">
                            <label class="col-sm-2 control-label">图片地址：</label>
                            <div class="col-sm-6">
                                <input type="text" name="url" class="form-control" placeholder="请上传图片或输入外链" />
                            </div>
                            <div class="col-sm-1">
                                <span id="btn-upfile1" class="btn btn-warning btn-sm" style="margin-bottom:0;padding:6px 8px;">
                                    <i class="glyphicon glyphicon-folder-open" style="padding-right:3px;"></i> 选择上传<input type="hidden" name="image_cover1" id="image_cover1" /><input id="file" class="file" type="file" name="pic" suffix="cover1" />
                                </span>
                            </div>
                            <div class="col-sm-1">
                                <img id="previewImg_cover1" style="height:34px;float: right;">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <input type="hidden" id = 'tag' name="tag" value="{$tag}"/>

                        <div class="form-group">
                            <div class="col-sm-12 col-sm-offset-5">
                                <button class="btn btn-primary" id="ok" type="button">提交</button>
                                <button class="btn btn-white" id="cancel" type="button">取消</button>
                            </div>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}